package com.zhixiang.core.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.collection.PersistentBag;
import org.hibernate.proxy.map.MapProxy;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

@SuppressWarnings({"rawtypes","unchecked","unused"})
public class BeanUtil
{
  private static Log logger = LogFactory.getLog(BeanUtil.class);

  public static ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();

  public static void copyNotNullProperties(Object obj, Object obj1)
    throws IllegalAccessException, InvocationTargetException
  {
    BeanUtilsBean beanutilsbean = BeanUtilsBean.getInstance();
    if (obj == null)
      throw new IllegalArgumentException("No destination bean specified");
    if (obj1 == null)
      throw new IllegalArgumentException("No origin bean specified");
    if (logger.isDebugEnabled())
      logger.debug("BeanUtils.copyProperties(" + obj + ", " + obj1 + ")");
    if ((obj1 instanceof DynaBean))
    {
      DynaProperty[] adynaproperty = ((DynaBean)obj1).getDynaClass().getDynaProperties();
      for (int i = 0; i < adynaproperty.length; i++)
      {
        String s = adynaproperty[i].getName();
        if ((!beanutilsbean.getPropertyUtils().isReadable(obj1, s)) || (!beanutilsbean.getPropertyUtils().isWriteable(obj, s)))
          continue;
        Object obj2 = ((DynaBean)obj1).get(s);
        beanutilsbean.copyProperty(obj, s, obj2);
      }
    }
    else
    {
      if ((obj1 instanceof Map))
      {
        Iterator iterator = ((Map)obj1).entrySet().iterator();

        while (iterator.hasNext())
        {
          Map.Entry entry = (Map.Entry)iterator.next();
          String s1 = (String)entry.getKey();
          if (beanutilsbean.getPropertyUtils().isWriteable(obj, s1)) {
            beanutilsbean.copyProperty(obj, s1, entry.getValue());
          }
        }
      }
      PropertyDescriptor[] apropertydescriptor = beanutilsbean.getPropertyUtils().getPropertyDescriptors(obj1);
      for (int j = 0; j < apropertydescriptor.length; j++)
      {
        String s2 = apropertydescriptor[j].getName();
        if (("class".equals(s2)) || (!beanutilsbean.getPropertyUtils().isReadable(obj1, s2)) || (!beanutilsbean.getPropertyUtils().isWriteable(obj, s2)))
          continue;
        try
        {
          Object obj3 = beanutilsbean.getPropertyUtils().getSimpleProperty(obj1, s2);
          if (obj3 != null)
            beanutilsbean.copyProperty(obj, s2, obj3);
        }
        catch (NoSuchMethodException localNoSuchMethodException)
        {
        }
      }
    }
  }

  public static Object populateEntity(HttpServletRequest httpservletrequest, Object obj, String s)
    throws IllegalAccessException, InvocationTargetException
  {
    HashMap hashmap = new HashMap();
    String s1;
    String s2;
    for (Enumeration enumeration = httpservletrequest.getParameterNames(); enumeration.hasMoreElements(); hashmap.put(s2, httpservletrequest.getParameterValues(s1)))
    {
      s1 = (String)enumeration.nextElement();
      s2 = s1;
      if (StringUtils.isNotEmpty(s)) {
        s2 = s2.replace(s + ".", "");
      }
    }
    getBeanUtils().populate(obj, hashmap);
    return obj;
  }
  public static Object convertValue(ConvertUtilsBean convertutilsbean, Object obj, Class class1)
  {
    Converter converter = convertutilsbean.lookup(class1);
    if (converter == null)
      return obj;
    Object obj1 = null;
    if ((obj instanceof String)) {
      obj1 = converter.convert(class1, (String)obj);
    }
    else if ((obj instanceof String[]))
      obj1 = converter.convert(class1, ((String[])obj)[0]);
    else
      obj1 = converter.convert(class1, obj);
    return obj1;
  }

  public static BeanUtilsBean getBeanUtils()
  {
    BeanUtilsBean beanutilsbean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
    return beanutilsbean;
  }

  public static Map getMapFromRequest(HttpServletRequest httpservletrequest)
  {
    Map map = httpservletrequest.getParameterMap();
    HashMap hashmap = new HashMap();
    for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); )
    {
      Map.Entry entry = (Map.Entry)iterator.next();
      String s = (String)entry.getKey();
      String[] as = (String[])entry.getValue();
      if (as.length == 1)
        hashmap.put(s, as[0]);
      else {
        hashmap.put(s, as);
      }
    }
    return hashmap;
  }
}

